#!/bin/sh
#
# openct       Script for starting and stopping OpenCT
#
# chkconfig:   2345 24 89
# description: OpenCT is a middleware framework for smart card terminals. \
#              This script starts and stops the smart card terminal handlers.
#
# processname: ifdhandler
# config:      /etc/openct.conf

### BEGIN INIT INFO
# Provides: openct
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Should-Start: $syslog $network
# Should-Stop: $syslog $network
# Short-Description: Middleware framework for smart card terminals
# Description: OpenCT is a middleware framework for smart card terminals.
#              This script starts and stops the smart card terminal handlers.
### END INIT INFO

. /etc/rc.d/init.d/functions

exec="/usr/sbin/openct-control"
config="/etc/openct.conf"
prog=openct
proc=ifdhandler

OPENCT_OPTIONS=
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    action $"Initializing OpenCT smart card terminals: " \
        $exec $OPENCT_OPTIONS init
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    action $"Stopping OpenCT smart card terminals: " \
            $exec $OPENCT_OPTIONS shutdown
    retval=$?
    echo
    if [ $retval -eq 0 ] ; then
        rm -f /var/run/openct/status
        rm -f $lockfile
    fi
    return $retval
}

restart() {
    stop
    start
}

oct_status_q() {
    if ! $exec $OPENCT_OPTIONS status >/dev/null 2>&1; then
        [ -f $lockfile ] && return 2
        [ -f /var/run/openct/status ] && return 1
        return 3
    fi
}

oct_status() {
    status $proc
    oct_status_q
    retval=$?
    if [ -e /var/run/openct/status ] ; then
        $exec $OPENCT_OPTIONS status
        [ -e /var/run/openct/status ] && \
            echo $"Waiting for reader attach/detach events..."
    fi
    return $retval
}

case "$1" in
    start)
        oct_status_q && exit 0
        $1
        ;;
    stop)
        oct_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        oct_status_q || exit 7
        restart
        ;;
    force-reload)
        restart
        ;;
    status)
        oct_status
        ;;
    try-restart|condrestart)
        oct_status_q || exit 0
        restart
	;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|condrestart|reload|force-reload}"
        exit 2
esac
exit $?
